package com.dataextend.service;

import com.alibaba.fastjson.JSONObject;
import com.dataextend.entity.*;
import com.dataextend.mapper.cluster.MarketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 *  邮件接口  模拟数据需对接腾讯云API
 */
@Service
public class EmailService {
    @Autowired
    MarketMapper marketMapper;


    public JSONObject createGroup(EmailRequest emailrequest){
        // 当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = df.format(new Date());
        String creater = "管理员";

        // 插入一条推送记录
        if (emailrequest.getPushPattern().equals("立即推送")){
            marketMapper.emailInfoInsert(emailrequest.getGroupId(), emailrequest.getGroupName(), emailrequest.getMissionName(),emailrequest.getEmailName(),
                    emailrequest.getMessageInfo(), emailrequest.getSenderEmail(), emailrequest.getPushPattern(), creater,
                    nowTime, emailrequest.getPushEndTime(), nowTime);
        } else {
            marketMapper.emailInfoInsert(emailrequest.getGroupId(), emailrequest.getGroupName(), emailrequest.getMissionName(),emailrequest.getEmailName(),
                    emailrequest.getMessageInfo(), emailrequest.getSenderEmail(), emailrequest.getPushPattern(), creater,
                    emailrequest.getPushStartTime(), emailrequest.getPushEndTime(), nowTime);
        }


        // 插入一条该记录的模拟推送结果数据
        try {
            Random r = new Random();
            // 发送人数
            int targetNum = r.nextInt(200) + 2000;
            // 触达人数
            int sendNum = r.nextInt(100) + 1750;
            // 点击人数
            int clickNum = r.nextInt(50) + 300;
            // 订单金额
            int dealNum = r.nextInt(1000) + 13000;

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            long startDate_ = 0L;
            String startDate;
            String endDate;
            if (emailrequest.getPushPattern().equals("立即推送")){
                startDate_ =  simpleDateFormat.parse(nowTime).getTime();
                startDate = nowTime.substring(0,10);
                endDate = simpleDateFormat.format(new Date(startDate_ + 86400000*3));
                marketMapper.emailResultInsert(emailrequest.getGroupId(), emailrequest.getGroupName(), nowTime, nowTime,
                        startDate, endDate, targetNum, sendNum, clickNum, dealNum);
            } else {
                startDate_ = simpleDateFormat.parse(emailrequest.getPushStartTime()).getTime();
                startDate = emailrequest.getPushStartTime().substring(0,10);
                endDate = simpleDateFormat.format(new Date(startDate_ + 86400000*3));
                marketMapper.emailResultInsert(emailrequest.getGroupId(), emailrequest.getGroupName(), nowTime,emailrequest.getPushStartTime(),
                        startDate, endDate, targetNum, sendNum, clickNum, dealNum);
            }

        } catch (Exception e){
            e.printStackTrace();
        }

        return new JSONObject();
    }


}
